<template>
  <div class="home-page">
    <van-tabs animated v-model="activeIndex">
      <van-tab v-for="item in channelsList" :key="item.id" :title="item.name">
        <ArticleList :id="item.id"></ArticleList>
      </van-tab>
    </van-tabs>
    <!-- 按钮 -->
    <div class="btn-wrapper">
      <geek-icon name="search"></geek-icon>
      <geek-icon name="channel" @click.native="showTime"></geek-icon>
    </div>
    <ArticleChannel
      :list="channelsList"
      ref="channel"
      :activeIndex.sync="activeIndex"
    ></ArticleChannel>
  </div>
</template>
<script>
import { channels } from '@/api/home.js'
import ArticleList from './components/article-list.vue'
import ArticleChannel from './components/article-channel.vue'

export default {
  name: 'Home',
  components: { ArticleList, ArticleChannel },
  data () {
    return {
      channelsList: [],
      activeIndex: 0
    }
  },
  methods: {
    showTime () {
      this.$refs.channel.isShow = true
    }
  },
  watch: {
    '$store.state.user.token': {
      async handler (newVal) {
        if (newVal) {
          const res = await channels()
          // console.log(res)
          this.channelsList = res
          this.activeIndex = 0
        }
      }
    }
  },
  // 获取频道
  async created () {
    const res = await channels()
    // console.log('res', res)
    this.channelsList = res
  }
}
</script>
<style scoped lang="less">
.home-page {
  .btn-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    width: 86px;
    height: 44px;
    background: #fff;
    display: flex;
    align-items: center;
    .geek-icon {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    &::before {
      content: '';
      width: 20px;
      height: 44px;
      position: absolute;
      left: -20px;
      top: 0;
      background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff);
    }
  }
}
::v-deep .van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  .van-tabs__line {
    background: @geek-color;
    height: 2px;
    width: 32px;
  }
  .van-tab {
    color: #9ea1ae;
  }
  .van-tab--active {
    font-size: 18px;
    color: #333;
  }
  .van-tabs__wrap {
    padding-right: 86px;
  }
  .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  .van-tab__pane {
    height: 100%;
  }
}
</style>
